package com.example.votemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.votemanage.entity.VoteItem;
import com.example.votemanage.entity.VoteOption;
import com.example.votemanage.entity.VoteSubject;
import com.example.votemanage.entity.VoteUser;
import com.example.votemanage.service.IVoteItemService;
import com.example.votemanage.service.IVoteOptionService;
import com.example.votemanage.service.IVoteSubjectService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class VoteItemController {

    @Autowired
    IVoteSubjectService voteSubjectService;

    @Autowired
    IVoteOptionService voteOptionService;
    @Autowired
    IVoteItemService voteItemService;

    @RequestMapping("/insert")
    public String voteOption() {
        return "add";
    }
    @RequestMapping("/update")
    public String voteOptionUpdate(@RequestParam("vsId") Integer vsId,
                                   HttpServletRequest request) {
        VoteSubject voteSubject = voteSubjectService.selectAllSubject(vsId);
        request.setAttribute("voteSubject", voteSubject);
        return "add";
    }
    @PostMapping("/insert")
    public String insert(@RequestParam("options") List<String> options,
                         @RequestParam("vsTitle") String vsTitle,
                         @RequestParam("vsType") int vsType) {
        // 创建并保存投票主题
        VoteSubject voteSubject = new VoteSubject();
        voteSubject.setVsTitle(vsTitle);
        voteSubject.setVsType(vsType);
        voteSubjectService.save(voteSubject);
        // 保存每一个选项
        for (int i = 0; i < options.size(); i++) {
            VoteOption voteOption=new VoteOption();
            voteOption.setVoOption(options.get(i));
            voteOption.setVsId(voteSubject.getVsId());
            voteOption.setVoOrder(i);
            voteOptionService.save(voteOption);
        }
        return "redirect:/addsuccess";
    }
    @PostMapping("/update")
    public String update(@RequestParam("options") List<String> options,
                         @RequestParam("vsTitle") String vsTitle,
                         @RequestParam("vsType") int vsType,
                         @RequestParam("vsId") Integer vsId,
                         @RequestParam(value = "voId", required = false) Integer[] voIds,
                         @RequestParam("deletedIds") String deletedIds) {
        // 更新投票主题
        VoteSubject voteSubject = new VoteSubject();
        voteSubject.setVsTitle(vsTitle);
        voteSubject.setVsType(vsType);
        QueryWrapper<VoteSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("VS_ID", vsId);
        voteSubjectService.update(voteSubject, queryWrapper);

        // 更新或新增投票选项
        for (int i = 0; i < options.size(); i++) {
            VoteOption voteOption = new VoteOption();
            voteOption.setVoOption(options.get(i));
            voteOption.setVsId(vsId);
            voteOption.setVoOrder(i);
            if (voIds != null && i < voIds.length && voIds[i] != null) {
                // 更新已有选项
                voteOption.setVoId(voIds[i]);
                voteOptionService.updateById(voteOption);
            } else {
                // 新增新选项
                voteOptionService.save(voteOption);
            }
        }

        // 删除指定的投票选项
        if (deletedIds != null && !deletedIds.isEmpty()) {
            String[] idsToDelete = deletedIds.split(",");
            for (String id : idsToDelete) {
                if (id != null && !id.trim().isEmpty()) {
                    voteOptionService.removeById(Integer.parseInt(id.trim()));
                }
            }
        }

        return "redirect:/manage";
    }

    @RequestMapping("/vote")
    public String vote(int vsid, HttpServletRequest request){
        VoteSubject voteSubject=voteSubjectService.selectAllSubject(vsid);
        request.setAttribute("voteSubject",voteSubject);
        return "vote";
    }
    @PostMapping("/vote")
    public String vote(@RequestParam("voId") Integer[] voIds,
                       @RequestParam("vsId") Integer vsId,
                       @RequestParam("vuUserId") String vuUserId) {
        for (Integer voId : voIds) {
            VoteItem voteItem = new VoteItem();
            voteItem.setVoId(voId);
            voteItem.setVsId(vsId);
            voteItem.setVuUserId(vuUserId);
            voteItemService.save(voteItem);
        }
        return "redirect:/success?vsId="+vsId;
    }
    @RequestMapping("/success")
    public String vote_success(int vsId,HttpServletRequest request){
        request.setAttribute("vsId",vsId);
        return "vote_success";
    }
}